package com.huaxi.ddgw.manager.service.impl;

import cn.hutool.core.date.DateUtil;
import com.huaxi.ddgw.manager.mapper.OrderInfoMapper;
import com.huaxi.ddgw.manager.mapper.OrderItemMapper;
import com.huaxi.ddgw.manager.mapper.OrderStatisticsMapper;
import com.huaxi.ddgw.manager.mapper.RegionMapper;
import com.huaxi.ddgw.manager.service.OrderInfoService;
import com.huaxi.ddgw.model.dto.h5.OrderInfoDto;
import com.huaxi.ddgw.model.dto.order.OrderStatisticsDto;
import com.huaxi.ddgw.model.entity.order.OrderInfo;
import com.huaxi.ddgw.model.entity.order.OrderItem;
import com.huaxi.ddgw.model.entity.order.OrderStatistics;
import com.huaxi.ddgw.model.vo.order.OrderStatisticsVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderInfoServiceImpl implements OrderInfoService {

    @Autowired
    private OrderStatisticsMapper orderStatisticsMapper;

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private RegionMapper regionMapper;

    //返回统计数据
    @Override
    public OrderStatisticsVo getOrderStatisticsData(OrderStatisticsDto orderStatisticsDto) {
        //根据dto条件查询统计结果数据 返回list集合
        List<OrderStatistics> orderStatisticsList = orderStatisticsMapper.selectList(orderStatisticsDto);
        //遍历list集合 得到所有日期 把所有日期封装到list新集合里面
        List<String> dateList = orderStatisticsList.stream()
                .map(orderStatistics -> DateUtil.format(orderStatistics.getOrderDate(), "yyyy-MM-dd"))
                .collect(Collectors.toList());
        //遍历list集合 得到所有日期对应的总金额 把总金额封装到list新集合里面
        List<BigDecimal> decimalList = orderStatisticsList.stream().map(OrderStatistics::getTotalAmount).collect(Collectors.toList());
        //把两个list集合封装到OrderStatisticsVo 返回OrderStatisticsVo
        OrderStatisticsVo orderStatisticsVo = new OrderStatisticsVo();
        orderStatisticsVo.setDateList(dateList);
        orderStatisticsVo.setAmountList(decimalList);
        return orderStatisticsVo;
    }

    //订单列表
    @Override
    public PageInfo<OrderInfo> findByPage(Integer page, Integer limit, OrderInfoDto orderInfoDto) {
        PageHelper.startPage(page, limit);
        List<OrderInfo> orderInfoList = orderInfoMapper.findByPage(orderInfoDto);
        PageInfo<OrderInfo> pageInfo = new PageInfo<>(orderInfoList);
        return pageInfo;
    }

    //订单详情
    @Override
    public List<OrderInfo> findOrderInfo(String orderNo) {
        List<OrderItem> orderItemList = orderItemMapper.findOrderItem(orderNo);
        List<OrderInfo> orderInfoList = orderInfoMapper.findOrderInfo(orderNo);
        orderInfoList.stream().forEach(item -> {
            item.setOrderItemList(orderItemList);
        });
        orderInfoList.stream().forEach(item -> {
            //拼接全部地址
            String provinceCode = regionMapper.selectByCode(item.getReceiverProvince());
            String cityCode = regionMapper.selectByCode(item.getReceiverCity());
            String districtCode = regionMapper.selectByCode(item.getReceiverDistrict());

            StringBuilder fullAddressBuilder = new StringBuilder();
            fullAddressBuilder.append(provinceCode).append(cityCode).append(districtCode).append(item.getReceiverAddress());
            String fullAddress = fullAddressBuilder.toString();
            item.setReceiverAddress(fullAddress);
        });
        return orderInfoList;
    }
}
